/**
 * 
 */
package uk.ac.lkl.lpp.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


import org.jdesktop.jdic.desktop.Desktop;

import uk.ac.lkl.common.util.Resources;
import uk.ac.lkl.common.util.ds.AbstractNotifyingList;
import uk.ac.lkl.common.util.ds.Listable;
import uk.ac.lkl.lpp.model.ldo.LearningEntity;

/**
 * @author yish
 *
 */
public class EntityNameButton extends JButton 
				implements ChangeListener, ActionListener, Listable {

	private static final Logger logger = Logger.getLogger("uk.ac.lkl.lpp");
	protected static String locator = Messages.class.getName();
	protected static ResourceBundle bundle = PropertyResourceBundle.getBundle(locator);;
    private static String LINK_ICON_LOCATION = "gfx/link.gif";
    public static final ImageIcon LINK_ICON    = Resources.getIcon(LINK_ICON_LOCATION); 
    
    private JPopupMenu pop = new JPopupMenu();
    private LearningEntity le;
    private AbstractNotifyingList myList;
    
    private int minchars = -1; // -1 = unlimited.
    private int maxchars = -1; // -1 = unlimited.
    


    /**	private final static class LinkActionListener implements ActionListener {
		private final String link;
		
		public LinkActionListener(String link) {
			this.link = link;
		}

        public void actionPerformed(ActionEvent e) {
            try {
                Desktop.browse(new URL(link)); //Throws an error
            }
            catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    }

     * TODO: add menuitems for move, delete, browse, and make menu popup on hover.
     */ 
    public EntityNameButton (){
    	setBorder(BorderFactory.createEtchedBorder());
    	JMenuItem editMI = new JMenuItem("Edit");
    	editMI.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			openEditor();
    		}
    	});
    	pop.add(editMI);
    	setComponentPopupMenu(pop);
		addActionListener(this);
    }
    
    public EntityNameButton (LearningEntity le){
    	this();
    	setEntity(le);
    }
    
    /**
     * TODO: this should look at pixels, not char counts.
     */ 
    public EntityNameButton (LearningEntity le, int maxchars){
    	this(le);
    	setMaxchars(maxchars);
    }
    
    /**
     * TODO: this should look at pixels, not char counts.
     */ 
    public EntityNameButton (LearningEntity le, int minchars, int maxchars){
    	this(le);
    	setMaxchars(maxchars);
    	setMinchars(minchars);
    }

    public int getMinchars() {
		return minchars;
	}

	public void setMinchars(int minchars) {
		this.minchars = minchars;
		updateDetails();
	}

	public int getMaxchars() {
		return maxchars;
	}

	public void setMaxchars(int maxchars) {
		this.maxchars = maxchars;
		updateDetails();
	}

    private void openEditor() {
		LearningEntityEditor.getEditorDialog(le, getLocationOnScreen()).setVisible(true);
    }
    
    public void setEntity(LearningEntity le){
    	if (this.le != null) {
    		this.le.removeChangeListener(this);
    	}
    	this.le = le;
		le.addChangeListener(this);
		updateDetails();    	
    }

	private void updateDetails() {
		String s = le.getTitle();
		if (minchars > 0 && s != null && s.length() < minchars) {
			char[] cs = new char[minchars - s.length()];
			for (int i=0; i<cs.length; i++) cs[i]=' ';
			s = s+new String(cs);
		} else if (maxchars > 0 && s != null && s.length() > maxchars) {
			s = s.substring(0,maxchars-3)+"...";			
		}
		setText(s);
		s = le.getDescription();
		if (s == null || s.length() < 3) s = le.getLink(); 
		if (s == null || s.length() < 3) s = le.getTitle();
		setToolTipText(s);

		validate();
	}
	
	

	public void stateChanged(ChangeEvent e){
		updateDetails();
	}			
	
	public void actionPerformed(ActionEvent e) {
		String link = le.getLink(); 
		if (link == null || !link.startsWith("http://")) {
			//getComponentPopupMenu().setVisible(true);
			return;
		}
		
        try {
            Desktop.browse(new URL(link));
        }
        catch (Exception exc) {
            exc.printStackTrace();
        }
    }

	public AbstractNotifyingList getMyList() {
		return myList;
	}

	public void setMyList(AbstractNotifyingList myList) {
		this.myList = myList;
	}
	
	public void addToList(AbstractNotifyingList l){
		l.add(this);
		setMyList(l);
	}
	
	public void removeFromList(AbstractNotifyingList l){
		l.remove(this);
		setMyList(null);
	}

}
